// packageB/pages/kaihei/kaihei.js
const app = getApp()
import {formatTime1} from "../../../utils/util.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appurl: app.apiurl,
    apiimg: app.apiimg,
    ggid: '',
    gglist: '',
    list:'',
    guuanj:'',
    days: 0,
    hours: 0,
    minutes: 0,
    seconds: 0,
    days1: 0,
    hours1: 0,
    minutes1: 0,
    seconds1: 0,
    tanchuang:false,
    guuanj_bssm_str:'',
    guuanj_jpsz_str:'',
    tanchuang11_flag: false,
    tanchuang21: false,
    show_last_flag:true,
    last_gjs_id:0,
    start_time:'',
    end_time:'',
    paimingList:[],
    idx:1,
    autoplay: true,
    duration: 500,
    wanshanziliao:false,
    sp_ren_img1:false,
    shujuxxx:'',
    sp_ren_img2:false,
    sp_ren_img3:false,
    sp_ren_img4:false,
    sp_ren_img5:false,
    yonghu:'',
    bs_url:'',
    array1: [
      '微信区', 'QQ区'
    ],
    index1: 0,
    array2: [],
    index2: 0,
    array4: ['对抗路', '中路', '发育路', '打野', '辅助'],
    index4: 0,
    dw1: true,
    dw2: true,
    dw4: true,
    gglist2:'',
    wzsb_name:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    if(options.gjs_id){
      that.setData({
        show_last_flag:false,
        last_gjs_id:options.gjs_id
      })
    }else{
      that.setData({
        show_last_flag:true,
        last_gjs_id:0
      })
    }
    this.z_guanjun()

  },
  z_tapbar(e) {
    let index = e.currentTarget.dataset.id
    this.setData({
      idx: index,
      page_no:1
    })
    this.z_jilu()
    this.z_jilu1()
  },
  z_open(){
    this.setData({
      tanchuang:true
    })
  },
  z_close(){
    this.setData({
      tanchuang:false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.z_peizhi()
    this.guanggao1()
    this.gameduanwei()
    this.z_yonghu()
    // this.z_guanjun()
    this.z_jilu3()
    this.z_jilu()
    this.z_jilu1()
    this.z_bsInfoData()
    this.z_kefu()
    this.guanggao3()
  },
  gameduanwei() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/gameDw?game_type_id=1',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        let array2 = _this.data.array2
        for (let i = 0; i < res.data.data.length; i++) {
          array2.push(res.data.data[i].name)
        }
        _this.setData({
          duanwei: res.data.data,
          array2: array2,
        })
      }
    })
  },
  guanggao3() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advert?page_id=22',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          gglist2: res.data.data[0]
        })
      }
    })
  },
  z_kefu() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/wztzs/tzsList',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          kefu: res.data.data
        })
      }
    })
  },
  z_bsInfoData() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "wztzs_id":_this.data.last_gjs_id
    }
    wx.request({
      url: app.apiurl + 'api/home/bsInfoData',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          shujuxxx:res.data.data
        })
      }
    })
  },
  z_peizhi() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/configBase',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          peizhi: res.data.data
        })
      }
    })
  },
  guanggao1() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advertPage',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          ggid: res.data.data[0].id
        })
        _this.guanggao2()
      }
    })
  },
  guanggao2() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advert?page_id=20',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          gglist: res.data.data[0]
        })
      }
    })
  },
  z_kvlianjia(e){
    let _this = this
    let link_url = e.currentTarget.dataset.url
    let id = e.currentTarget.dataset.url
    if(link_url != undefined){
      wx.navigateTo({
        url: link_url,
      })
    }
  },
  z_guanjun() {
    let _this = this
    let last_gjs_id = _this.data.last_gjs_id
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + '/api/wztzs/tzsInfo?wztzs_id='+last_gjs_id,
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        let bssm_str = res.data.data.bssm_str;
        let jpsz_str = res.data.data.jpsz_str;
        function replaceColorToStyle(str) {
          const regex = /<font color=["'](.*?)["']/g;
          return str.replace(regex, '<font style="color:$1"');
        }
        bssm_str = replaceColorToStyle(bssm_str);
        jpsz_str = replaceColorToStyle(jpsz_str);
        _this.setData({
          guuanj: res.data.data,
          guuanj_bssm_str:bssm_str,
          guuanj_jpsz_str:jpsz_str
        })
        wx.setNavigationBarTitle({
          title: res.data.data.top_title,
          });
      }
    })
  },
  z_baoming() {
    let _this = this
    if (wx.getStorageSync("user_token") == "") {
      _this.z_dengluyanzeng()
    } else {
      _this.setData({
        sp_ren_img5:true
      })
    }
  },
z_dengluyanzeng(){
  wx.request({
    url: app.apiurl + 'api/home/xcxLoginStatus',
    method: 'GET',
    data: {},
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 修改请求
    },
    success(res) {
      if (res.data.code == '1001') {
        if (res.data.data.is_force_login == '1') {
          // wx.reLaunch({
          //   url: '/pages/login/login'
          // });
          wx.showToast({
            title: '请注册/登录会员账号',
            icon: 'none',
            duration: 2000
          })
          setTimeout(function(){
            wx.reLaunch({
              url: '/pages/member/member',
            })
          },2500)
        }
      }
    }
  })
},
z_xiugai1() {
  let _this = this
  let params = {
    "token": wx.getStorageSync("user_token"),
    "head_img": _this.data.yonghu.head_img,
    "game_name": _this.data.yonghu.name,
    "game_area": Number(_this.data.index1) + 1,
    "game_dw": _this.data.duanwei[_this.data.index2].id,
    "sex": _this.data.yonghu.sex,
    "refereeid": _this.data.yonghu.refereeid,
    "scfl_num": Number(_this.data.index4) + 1,
    "phone_type": _this.data.yonghu.phone_type,
    "wzsb_name": _this.data.wzsb_name,
  }
 if (_this.data.dw1) {
    wx.showToast({
      title: '请选择游戏区',
      icon: 'none',
      duration: 2000
    })
  } else if (_this.data.dw2) {
    wx.showToast({
      title: '请选择游戏段位',
      icon: 'none',
      duration: 2000
    })
  } else if (_this.data.dw4) {
    wx.showToast({
      title: '请选择擅长分路',
      icon: 'none',
      duration: 2000
    })
  } else if (_this.data.name == '') {
    wx.showToast({
      title: '请填写王者赛宝账号昵称',
      icon: 'none',
      duration: 2000
    })
  }  else {
    wx.request({
      url: app.apiurl + 'api/member/upGameLevel',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          sp_ren_img5:false
        })
        _this.z_baoming1()
      }
    })
  }
},
  z_baoming1() {
    let _this = this
    let gjs_id = _this.data.last_gjs_id
    let params = {
      "token": wx.getStorageSync("user_token"),
      "wztzs_id":gjs_id,
    }
    wx.request({
      url: app.apiurl + 'api/wztzs/baoming',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
      if(res.data.code == '1001'){
        _this.setData({
          sp_ren_img3:true
        })
       _this.z_guanjun()
       wx.showToast({
        title: res.data.msg,
        icon: 'none',
        duration: 2000
      })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
        setTimeout(function(){
          wx.navigateTo({
            url: '/pages/choujiang1/choujiang1',
          })
        },2000)
      }
      }
    })
  },
  copyText(e) {
    let _this = this;
    let textToCopy = String(e.currentTarget.dataset.id);
    wx.setClipboardData({
      data: textToCopy,
      success(res) {
        wx.showToast({
          title: '复制成功',
          icon: 'success'
        });
      },
      fail(err) {
        console.error('复制失败:', err);
        wx.showToast({
          title: '复制失败',
          icon: 'none'
        });
      }
    });
  },
  fuzhi1() {
    let _this = this
    wx.setClipboardData({
      data: _this.data.yonghu.id,
      success(res) {
        debugger
        wx.getClipboardData({
          success(res) {
            console.log(res.data) // data
            wx.showToast({
              title: '复制成功',
              icon: 'success',
              duration: 2000
            })
          }
        })
      }
    })
  },
  z_open1(){
    this.setData({
      wanshanziliao:true
    })
  },
  z_close1(){
    this.setData({
      wanshanziliao:false
    })
  },
  // z_url(){
  //   let link = encodeURIComponent(this.data.guuanj.link);
  //   link = decodeURIComponent(link);
  //   wx.setStorage({
  //     key: "userInfo",
  //     data: { link: link}
  //   })
  //   wx.navigateTo({
  //     url: '/packageB/pages/danye/danye',
  //   })
  // },
  // z_url() {
  //   let _this = this
  //   wx.setClipboardData({
  //     data: _this.data.guuanj.link,
  //     success(res) {
  //       wx.getClipboardData({
  //         success(res) {
  //           console.log(res.data) // data
  //           wx.showToast({
  //             title: '复制成功，请在浏览器粘贴',
  //             icon: 'none',
  //             duration: 2000
  //           })
  //         }
  //       })
  //     }
  //   })
  // },
  z_url1(){
    this.setData({
      sp_ren_img2:true
    })
  },
  z_url2(){
    this.setData({
      sp_ren_img3:true
    })
  },
  z_url3(){
     wx.navigateTo({
      url: this.data.guuanj.bslc,
    })
  },
  close1(){
    this.setData({
      sp_ren_img2:false,
      sp_ren_img3:false,
      sp_ren_img4:false
    })
  },
  close2(){
    this.setData({
      sp_ren_img5:false
    })
  },
  z_shuaxin(){
    let _this = this
    _this.setData({
      guuanj:''
    })
    if(_this.data.show_last_flag){
      _this.setData({
        last_gjs_id:'0'
      })
    }
    _this.z_guanjun()
    _this.z_jilu()
    _this.z_jilu1()
  },
  countdown2: function (difftime1) {
    const that = this;
    let difftime = Number(difftime1);
    const interval = setInterval(function () {
      if (difftime > 0) {
        difftime--;
        var seconds = difftime;
        var days = Math.floor(seconds / (3600*24));
        seconds %= 3600*24;
     
        var hours = Math.floor(seconds / 3600);
        seconds %= 3600;
     
        var minutes = Math.floor(seconds / 60);
        seconds %= 60;
        that.setData({
          days: days,
          hours: hours,
          minutes: minutes,
          seconds: seconds,
        })
      } else {
        clearInterval(interval);
        that.z_guanjun();
        // 倒计时结束的操作
      }
    }, 1000);
  },
  countDown: function (difftime) {
    const that = this;
    let diff_time = Number(difftime) * 1000
    var endTime = new Date().getTime() + diff_time; 
    setInterval(function () {
      const nowTime = new Date().getTime();
      let distance = endTime - nowTime;
 
      const days = Math.floor(distance / (1000 * 60 * 60 * 24));
      const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      const seconds = Math.floor((distance % (1000 * 60)) / 1000);
      if(days < 0){
        days = 0;
      }
      if(hours < 0){
        hours = 0;
      }
      if(minutes < 0){
        minutes = 0;
      }
      if(seconds < 0){
        seconds = 0;
      }
      that.setData({
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds,
      });
    }, 1000);
  },
  countDown1: function () {
    const that = this;
    setInterval(function () {
      const nowTime = new Date().getTime();
      const endTime = that.data.guuanj.dif_time; 
      let distance = endTime - nowTime;
 
      const days = Math.floor(distance / (1000 * 60 * 60 * 24));
      const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      const seconds = Math.floor((distance % (1000 * 60)) / 1000);
 
      that.setData({
        days1: that.formatTime(days),
        hours1: that.formatTime(hours),
        minutes1: that.formatTime(minutes),
        seconds1: that.formatTime(seconds),
      });
    }, 1000);
  },
  formatTime: function (time) {
    return time < 10 ? '0' + time : time;
  },
  z_jilu3(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token")
    }
    wx.request({
      url: app.apiurl + 'api/config/unprizeList?fbwz_type=6',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          mylist:res.data.data
        })
      }
    })
   },
   z_jilu() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/member_kh/hjRecordList?page_no=1&page_num=100&prize_type=4',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          list: res.data.data.list
        })
      }
    })
  },
  z_jilu1() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/duobao/gjsCsRecord?page_no=1&page_num=100',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          list1: res.data.data.list
        })
      }
    })
  },
   z_yonghu() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/member/myInfo',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        if (res.data.code == '1001') {
          _this.setData({
            yonghu: res.data.data,
            wzsb_name: res.data.data.wzsb_name,
          })
          if (res.data.data.game_area > 0) {
            _this.setData({
              index1: _this.data.array1.indexOf(res.data.data.game_area_name),
              dw1: false
            })
          }
          if (res.data.data.game_dw_id > 0) {
            let index2 = _this.data.array2.indexOf(res.data.data.game_dw_name)
            if(index2 == '-1'){
              index2 = '0'
            }
            _this.setData({
              index2: index2,
              dw2: false
            })
          }
          if (res.data.data.scfl_num > 0) {
            _this.setData({
              index4: _this.data.array4.indexOf(res.data.data.scfl_num_name),
              dw4: false
            })
          }
        }
      }
    })
  },

  z_kaihei4() {
    let _this = this
    _this.setData({
      tanchuang11: false,
      tanchuang11_flag: true,
      tanchuang21: true
    })
  },
  z_kaihei5() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "id": wx.getStorageSync("guanjun")
    }
    wx.request({
      url: app.apiurl + 'api/wztzs/upWztzsTan',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          tanchuang11: false,
          tanchuang11_flag:false
        })
      }
    })
  },
  z_tiaozhuan(){
    let that = this;
    if(that.data.last_gjs_id == 0){
      wx.showToast({
        title: '暂无上期',
        icon: 'none',
        duration: 2000
      })
    }else{
      wx.navigateTo({
        url: '/packageB/pages/kaihei/kaihei?gjs_id='+that.data.last_gjs_id,
      })
    }
  },
  bindPickerChange1: function (e) {
    let _this = this
    _this.setData({
      index1: e.detail.value,
      dw1: false
    })
  },
  bindPickerChange2: function (e) {
    let _this = this
    _this.setData({
      index2: e.detail.value,
      dw2: false
    })
  },
  bindPickerChange4: function (e) {
    let _this = this
    _this.setData({
      index4: e.detail.value,
      dw4: false
    })
  },
  z_shuru1(e) {
    this.setData({
      wzsb_name: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})